{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Baseball Game"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #stack #array #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #栈 #数组 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: calPoints"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #棒球比赛"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成，过去几回合的得分可能会影响以后几回合的得分。</p>\n",
    "\n",
    "<p>比赛开始时，记录是空白的。你会得到一个记录操作的字符串列表 <code>ops</code>，其中 <code>ops[i]</code> 是你需要记录的第 <code>i</code> 项操作，<code>ops</code> 遵循下述规则：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>整数 <code>x</code> - 表示本回合新获得分数 <code>x</code></li>\n",
    "\t<li><code>\"+\"</code> - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。</li>\n",
    "\t<li><code>\"D\"</code> - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。</li>\n",
    "\t<li><code>\"C\"</code> - 表示前一次得分无效，将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。</li>\n",
    "</ol>\n",
    "\n",
    "<p>请你返回记录中所有得分的总和。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>ops = [\"5\",\"2\",\"C\",\"D\",\"+\"]\n",
    "<strong>输出：</strong>30\n",
    "<strong>解释：</strong>\n",
    "\"5\" - 记录加 5 ，记录现在是 [5]\n",
    "\"2\" - 记录加 2 ，记录现在是 [5, 2]\n",
    "\"C\" - 使前一次得分的记录无效并将其移除，记录现在是 [5].\n",
    "\"D\" - 记录加 2 * 5 = 10 ，记录现在是 [5, 10].\n",
    "\"+\" - 记录加 5 + 10 = 15 ，记录现在是 [5, 10, 15].\n",
    "所有得分的总和 5 + 10 + 15 = 30\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>ops = [\"5\",\"-2\",\"4\",\"C\",\"D\",\"9\",\"+\",\"+\"]\n",
    "<strong>输出：</strong>27\n",
    "<strong>解释：</strong>\n",
    "\"5\" - 记录加 5 ，记录现在是 [5]\n",
    "\"-2\" - 记录加 -2 ，记录现在是 [5, -2]\n",
    "\"4\" - 记录加 4 ，记录现在是 [5, -2, 4]\n",
    "\"C\" - 使前一次得分的记录无效并将其移除，记录现在是 [5, -2]\n",
    "\"D\" - 记录加 2 * -2 = -4 ，记录现在是 [5, -2, -4]\n",
    "\"9\" - 记录加 9 ，记录现在是 [5, -2, -4, 9]\n",
    "\"+\" - 记录加 -4 + 9 = 5 ，记录现在是 [5, -2, -4, 9, 5]\n",
    "\"+\" - 记录加 9 + 5 = 14 ，记录现在是 [5, -2, -4, 9, 5, 14]\n",
    "所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>ops = [\"1\"]\n",
    "<strong>输出：</strong>1\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= ops.length <= 1000</code></li>\n",
    "\t<li><code>ops[i]</code> 为 <code>\"C\"</code>、<code>\"D\"</code>、<code>\"+\"</code>，或者一个表示整数的字符串。整数范围是 <code>[-3 * 10<sup>4</sup>, 3 * 10<sup>4</sup>]</code></li>\n",
    "\t<li>对于 <code>\"+\"</code> 操作，题目数据保证记录此操作时前面总是存在两个有效的分数</li>\n",
    "\t<li>对于 <code>\"C\"</code> 和 <code>\"D\"</code> 操作，题目数据保证记录此操作时前面总是存在一个有效的分数</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [baseball-game](https://leetcode.cn/problems/baseball-game/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [baseball-game](https://leetcode.cn/problems/baseball-game/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"5\",\"2\",\"C\",\"D\",\"+\"]', '[\"5\",\"-2\",\"4\",\"C\",\"D\",\"9\",\"+\",\"+\"]', '[\"1\",\"C\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from functools import reduce\n",
    "class Solution:\n",
    "    def operate(o):\n",
    "        pass\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        ans = [0] * len(operations)\n",
    "        index = 0\n",
    "        for o in operations:\n",
    "            if o=='+':\n",
    "                ans[index] = ans[index-1] + ans[index-2]\n",
    "                index += 1\n",
    "            elif o=='D':\n",
    "                ans[index] = 2 * ans[index-1]\n",
    "                index += 1\n",
    "            elif o=='C':\n",
    "                ans[index-1] = 0\n",
    "                index -= 1\n",
    "            else:\n",
    "                ans[index] = int(o)\n",
    "                index += 1\n",
    "        return sum(ans)         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        st = list()\n",
    "        for op in operations:\n",
    "            if op == '+':\n",
    "                st.append(st[-1] + st[-2])\n",
    "            elif op == 'D':\n",
    "                st.append(st[-1] * 2)\n",
    "            elif op == 'C':\n",
    "                st.pop()\n",
    "            else:\n",
    "                st.append(int(op))\n",
    "        return sum(st)\n",
    "\n",
    "\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        st = list()\n",
    "        for op in operations:\n",
    "            if op =='+':\n",
    "                st.append(st[-1]+st[-2])\n",
    "            elif op =='D':\n",
    "                st.append(st[-1]*2)\n",
    "            elif op =='C':\n",
    "                st.pop()\n",
    "            else:\n",
    "                st.append(int(op))\n",
    "        return sum(st)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res=[]\n",
    "        for op in operations:\n",
    "            if op == \"+\":\n",
    "                res.append(res[-1]+res[-2])\n",
    "            elif op == \"D\":\n",
    "                res.append(2*res[-1])\n",
    "            elif op == \"C\":\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(int(op))\n",
    "        return sum(res)\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        result = []\n",
    "        for i in operations:\n",
    "            if i != 'C' and i != 'D' and i != '+':\n",
    "                result += [int(i)]\n",
    "            if i == 'C':\n",
    "                result.pop()\n",
    "            if i == 'D':\n",
    "                result += [int(result[-1]) * 2]\n",
    "            if i == '+':\n",
    "                result += [int(result[-1])+int(result[-2])]\n",
    "        k = 0\n",
    "        for j in result:\n",
    "            k += int(j)\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        lst_score = []\n",
    "        for s in operations:\n",
    "            if s == '+':\n",
    "                lst_score.append(lst_score[-2] + lst_score[-1])\n",
    "            elif s == 'D':\n",
    "                lst_score.append(lst_score[-1] * 2)\n",
    "            elif s == 'C':\n",
    "                lst_score.pop()\n",
    "            else:\n",
    "                lst_score.append(int(s))\n",
    "        return sum(lst_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, ops: List[str]) -> int:\n",
    "        records = []\n",
    "        for op in ops:\n",
    "            match op:\n",
    "                case \"D\":\n",
    "                    records.append(records[-1] * 2)\n",
    "                case \"C\":\n",
    "                    records.pop()\n",
    "                case \"+\":\n",
    "                    records.append(records[-2] + records[-1])\n",
    "                case _:\n",
    "                    records.append(int(op))\n",
    "        return sum(records)\n",
    "\n",
    "# 作者：Alex\n",
    "# 链接：https://leetcode.cn/problems/baseball-game/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res=[]\n",
    "        for op in operations:\n",
    "            if op == \"+\":\n",
    "                res.append(res[-1]+res[-2])\n",
    "            elif op == \"D\":\n",
    "                res.append(2*res[-1])\n",
    "            elif op == \"C\":\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(int(op))\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res=[]\n",
    "        for op in operations:\n",
    "            if op == \"+\":\n",
    "                res.append(res[-1]+res[-2])\n",
    "            elif op==\"D\":\n",
    "                res.append(2*res[-1])\n",
    "            elif op==\"C\":\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(int(op))\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        count = []\n",
    "        for i in operations:\n",
    "            if i.isalpha() == True:\n",
    "                if i == 'C':\n",
    "                    count.pop()\n",
    "                elif i == 'D':\n",
    "                    a = count[-1]*2\n",
    "                    count.append(a)\n",
    "            elif i == '+':\n",
    "                a = count[-1]\n",
    "                a = a + count[len(count)-2]\n",
    "                count.append(a)\n",
    "            else:\n",
    "                count.append(int(i))\n",
    "        return sum(count)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        rec = []\n",
    "        for op in operations:\n",
    "            if op == '+':\n",
    "                rec.append(rec[-1] + rec[-2])\n",
    "            elif op == 'D':\n",
    "                rec.append(rec[-1] * 2)\n",
    "            elif op == 'C':\n",
    "                rec.pop()\n",
    "            else:\n",
    "                rec.append(int(op))\n",
    "        return sum(rec)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        points = []\n",
    "        for o in operations:\n",
    "            if o == '+':\n",
    "                points.append(points[-1] + points[-2])\n",
    "            elif o == 'C':\n",
    "                points.pop()\n",
    "            elif o == 'D':\n",
    "                points.append(points[-1] * 2)\n",
    "            else:\n",
    "                points.append(int(o))\n",
    "        #\n",
    "        return sum(points)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        stack = []\n",
    "        for op in operations:\n",
    "            if op == '+':\n",
    "                stack.append(\n",
    "                    stack[-1] + stack[-2]\n",
    "                )\n",
    "            elif op == 'D':\n",
    "                stack.append(\n",
    "                    stack[-1] * 2\n",
    "                )\n",
    "            elif op == 'C':\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(int(op))\n",
    "\n",
    "        return sum(stack)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        ans = 0\n",
    "        stack = []\n",
    "        for i in operations:\n",
    "            if i == '+':\n",
    "                stack.append(stack[-1] + stack[-2])\n",
    "            elif i == 'D':\n",
    "                stack.append(stack[-1] * 2)\n",
    "            elif i == 'C':\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(int(i))\n",
    "        \n",
    "        return sum(stack)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        stack = []\n",
    "        for i in operations:\n",
    "            if i == '+':stack.append(stack[-1]+stack[-2])\n",
    "            elif i == 'D':stack.append(stack[-1]*2)\n",
    "            elif i == 'C':stack.pop()\n",
    "            else:stack.append(int(i))\n",
    "        return sum(stack)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res = []\n",
    "        for i in operations:\n",
    "            if i == 'C':\n",
    "                res.pop()\n",
    "            elif i == 'D':\n",
    "                res.append(res[-1] * 2)\n",
    "            elif i == '+':\n",
    "                res.append(res[-1] + res[-2])\n",
    "            else:\n",
    "                res.append(int(i))\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        j = 0\n",
    "        for i in range(len(operations)):\n",
    "            if operations[j] == 'C':\n",
    "                operations.pop(j)\n",
    "                operations.pop(j-1)\n",
    "                j -= 2\n",
    "            elif operations[j] == 'D':\n",
    "                operations[j] = int(operations[j-1])*2\n",
    "            elif operations[j] == '+':\n",
    "                operations[j] = int(operations[j-1]) + int(operations[j-2])\n",
    "            else:\n",
    "                operations[j] = int(operations[j])\n",
    "            j += 1\n",
    "        return sum(operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res=[]#定义空表res\n",
    "        for op in operations:#遍历\n",
    "            if op == \"+\":#若op是＋，res的最后两数相加，并添加到res\n",
    "                res.append(res[-1]+res[-2])\n",
    "            elif op==\"D\":#若op是D，res的最后一个数乘2，并添加到res\n",
    "                res.append(2*res[-1])\n",
    "            elif op==\"C\":#若op是C，res的最后一个数删掉\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(int(op))#op转换为整数并添加到res\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        points = []\n",
    "        for op in operations:\n",
    "            if op == '+':\n",
    "                points.append(points[-1] + points[-2])\n",
    "            elif op == 'C':\n",
    "                points.pop()\n",
    "            elif op == 'D':\n",
    "                points.append(points[-1] + points[-1])\n",
    "            else:\n",
    "                points.append(int(op))\n",
    "        #\n",
    "        return sum(points)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        pointslist = []\n",
    "        ops = ['C', 'D', '+']\n",
    "        scores = 0\n",
    "        for s in operations:\n",
    "            if s not in ops:\n",
    "                pointslist.append(int(s))\n",
    "            if s == 'C':\n",
    "                pointslist.pop()\n",
    "            if s == 'D':\n",
    "                currentpoint = pointslist[-1] * 2\n",
    "                pointslist.append(currentpoint)\n",
    "            if s == '+':\n",
    "                n = len(pointslist)\n",
    "                currentscore = pointslist[-1] + pointslist[n - 2]\n",
    "                pointslist.append(currentscore)\n",
    "        for i in pointslist:\n",
    "            scores += i\n",
    "        return scores\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 定义一个calPoints方法，接受一个包含操作符的列表作为参数，并返回一个整数结果\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        # 创建一个空列表res，用于存储每次操作后的结果值\n",
    "        res=[]\n",
    "        # 遍历操作符列表\n",
    "        for op in operations:\n",
    "            # 如果操作符是\"+\"，则将最后两个元素相加并将结果添加到res中\n",
    "            if op == \"+\":\n",
    "                res.append(res[-1]+res[-2])\n",
    "            # 如果操作符是\"D\"，则将最后元素的值加倍并将结果添加到res中\n",
    "            elif op==\"D\":\n",
    "                res.append(2*res[-1])\n",
    "            # 如果操作符是\"C\"，则从res中删除最后元素\n",
    "            elif op==\"C\":\n",
    "                res.pop()\n",
    "            # 如果操作符不是以上三种，则将其视为一个整数，并将其添加到res中\n",
    "            else:\n",
    "                res.append(int(op))\n",
    "        # 返回res中所有元素的和作为最终结果\n",
    "        return sum(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        stack = []\n",
    "        m = 0\n",
    "        stack.append(int(operations[0])) \n",
    "        for i in range(1,len(operations)):\n",
    "            if operations[i].isdigit():\n",
    "                stack.append(int(operations[i]))\n",
    "            elif operations[i].lstrip('-').isdigit():\n",
    "                stack.append(int(operations[i]))\n",
    "            elif operations[i] == 'C':\n",
    "                stack.pop()\n",
    "            elif operations[i] == 'D':\n",
    "                stack.append(stack[-1]*2)\n",
    "            else:\n",
    "                stack.append(stack[-1]+stack[-2])\n",
    "        while stack:\n",
    "            m += stack.pop()\n",
    "        print(stack)\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        count = []\n",
    "        for num in operations:\n",
    "            if num == '+':\n",
    "                prev1 = count[-1]\n",
    "                prev2 = count[-2]\n",
    "                count.append(prev1 + prev2)\n",
    "            elif num == 'D':\n",
    "                prev = count[-1]    \n",
    "                count.append(prev * 2)\n",
    "            elif num == 'C':\n",
    "                count.pop()\n",
    "            else:\n",
    "                count.append(int(num))\n",
    "        return sum(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        stack = []\n",
    "        i = 0\n",
    "        while i < len(operations):\n",
    "            if operations[i] == '+':\n",
    "                stack.append(stack[-2] + stack[-1])\n",
    "            elif operations[i] == 'C':\n",
    "                stack.pop()\n",
    "            elif operations[i] == 'D':\n",
    "                stack.append(stack[-1]*2)\n",
    "            else:\n",
    "                stack.append(int(operations[i]))\n",
    "            i += 1\n",
    "        return sum(stack)\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res=[]\n",
    "        for op in operations:\n",
    "            if op == \"+\":\n",
    "                res.append(res[-1]+res[-2])\n",
    "            elif op == \"D\":\n",
    "                res.append(2*res[-1])\n",
    "            elif op == \"C\":\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(int(op))\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res=[]\n",
    "        for op in operations:\n",
    "            if op == \"+\":\n",
    "                res.append(res[-1]+res[-2])\n",
    "            elif op==\"D\":\n",
    "                res.append(2*res[-1])\n",
    "            elif op==\"C\":\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(int(op))\n",
    "        return sum(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, ops: List[str]) -> int:\n",
    "        idx = 0\n",
    "        nums = [0]*1010\n",
    "        for i in ops:\n",
    "            if i == '+':\n",
    "                nums[idx] = nums[idx-1]+nums[idx-2]\n",
    "            elif i == 'D':\n",
    "                nums[idx] = nums[idx-1]*2\n",
    "            elif i == 'C':\n",
    "                idx -=2\n",
    "            else:\n",
    "                nums[idx] = int(i)\n",
    "            idx +=1\n",
    "        return sum(nums[:idx])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res=[]\n",
    "        for op in operations:\n",
    "            if op ==\"+\":\n",
    "                res.append(res[-1]+res[-2])\n",
    "            elif op ==\"D\":\n",
    "                res.append(2*res[-1])\n",
    "            elif op==\"C\":\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(int(op))\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        reslist=[]\n",
    "        for i in operations:\n",
    "            temp=0\n",
    "            if i==\"+\":\n",
    "                temp=reslist[-1]+reslist[-2]\n",
    "            elif i==\"D\":\n",
    "                temp=2*reslist[-1]\n",
    "            elif i==\"C\":\n",
    "                reslist.pop()\n",
    "                continue\n",
    "            else:\n",
    "                temp=int(i)\n",
    "            reslist.append(temp)\n",
    "        return sum(reslist)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        st=[]\n",
    "        for i in operations:\n",
    "            \n",
    "            if i ==\"C\":\n",
    "                st.pop()\n",
    "            elif i==\"D\":\n",
    "                k=st[-1]*2\n",
    "                st.append(k)\n",
    "            elif i==\"+\":\n",
    "                k=st[-1]+st[-2]\n",
    "                st.append(k)\n",
    "            else:\n",
    "                st.append(int(i))\n",
    "\n",
    "        return sum(st)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        points = []\n",
    "        for op in operations:\n",
    "            if op == '+':\n",
    "                points.append(points[-1] + points[-2])\n",
    "            elif op == 'C':\n",
    "                points.pop()\n",
    "            elif op == 'D':\n",
    "                points.append(points[-1] * 2)\n",
    "            else:\n",
    "                points.append(int(op))\n",
    "        #\n",
    "        return sum(points)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        record = []\n",
    "        for op in operations :\n",
    "            if op.isdigit()or (op[0] == '-' and op[1:].isdigit()):\n",
    "                record.append(int(op))\n",
    "            elif op == 'C':\n",
    "                record.pop()\n",
    "            elif op =='D':\n",
    "                record.append(record[-1]*2)\n",
    "            elif op == '+':\n",
    "                record.append(record[-1]+record[-2])\n",
    "        return sum(record)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        recode = []\n",
    "        for op in operations:\n",
    "            match op:\n",
    "                case '+':\n",
    "                    recode.append(recode[-2] + recode[-1])\n",
    "                case 'D':\n",
    "                    recode.append(recode[-1] * 2)\n",
    "                case 'C':\n",
    "                    recode.pop()\n",
    "                case _:\n",
    "                    recode.append(int(op))\n",
    "        return sum(recode)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res = []\n",
    "        prev1, prev2 = 0, 0\n",
    "        n = len(operations)\n",
    "        for i in range(n):\n",
    "            if operations[i] == '+':\n",
    "                res.append(res[-1] + res[-2])\n",
    "            elif operations[i] == 'D':\n",
    "                res.append(res[-1] * 2)\n",
    "            elif operations[i] == 'C':\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(int(operations[i]))\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res=[]\n",
    "        for op in operations:\n",
    "            if op == \"+\":\n",
    "                res.append(res[-1]+res[-2])\n",
    "            elif op==\"D\":\n",
    "                res.append(2*res[-1])\n",
    "            elif op==\"C\":\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(int(op))\n",
    "        return sum(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "nums = [0] * 1010\n",
    "class Solution:\n",
    "    def calPoints(self, ops: List[str]) -> int:\n",
    "        idx = 0\n",
    "        for x in ops:\n",
    "            if x == '+':\n",
    "                nums[idx] = nums[idx - 1] + nums[idx - 2]\n",
    "            elif x == 'D':\n",
    "                nums[idx] = nums[idx - 1] * 2\n",
    "            elif x == 'C':\n",
    "                idx -= 2\n",
    "            else:\n",
    "                nums[idx] = int(x)\n",
    "            idx += 1\n",
    "        return sum(nums[:idx])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: list[str]) -> int:\n",
    "        sums = 0\n",
    "        i = 0\n",
    "        while i < len(operations):\n",
    "            if operations[i] == 'C':\n",
    "                sums -= int(operations[i - 1])\n",
    "                operations.pop(i - 1)\n",
    "                operations.pop(i - 1)\n",
    "                i -= 2\n",
    "            elif operations[i] == 'D':\n",
    "                operations[i] = 2 * int(operations[i - 1])\n",
    "                sums += operations[i]\n",
    "            elif operations[i] == '+':\n",
    "                operations[i] = int(operations[i - 1]) + int(operations[i - 2])\n",
    "                sums += operations[i]\n",
    "            else:\n",
    "                sums += int(operations[i])\n",
    "            i += 1\n",
    "        return sums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        stack = []\n",
    "        for ch in operations:\n",
    "            if ch == \"C\" :\n",
    "                stack.pop()\n",
    "                continue\n",
    "            if ch == \"D\":\n",
    "                stack.append(stack[-1]*2)\n",
    "                continue\n",
    "            if ch == \"+\":\n",
    "                stack.append(stack[-1] + stack[-2])\n",
    "            \n",
    "            else:\n",
    "                stack.append(int(ch))\n",
    "        return sum(stack)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "nums = [0] * 1010\n",
    "class Solution:\n",
    "    def calPoints(self, ops: List[str]) -> int:\n",
    "        idx = 0\n",
    "        for x in ops:\n",
    "            if x == '+':\n",
    "                nums[idx] = nums[idx - 1] + nums[idx - 2]\n",
    "            elif x == 'D':\n",
    "                nums[idx] = nums[idx - 1] * 2\n",
    "            elif x == 'C':\n",
    "                idx -= 2\n",
    "            else:\n",
    "                nums[idx] = int(x)\n",
    "            idx += 1\n",
    "        return sum(nums[:idx])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res=[]\n",
    "        for ops in operations:\n",
    "            if ops == \"+\":\n",
    "                res.append(res[-1]+res[-2])\n",
    "            elif ops==\"D\":\n",
    "                res.append(2*res[-1])\n",
    "            elif ops==\"C\":\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(int(ops))\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, ops: List[str]) -> int:\n",
    "        res = []\n",
    "        for op in ops:\n",
    "            if op == \"C\":\n",
    "                res.remove(res[-1])\n",
    "            elif op == \"D\":\n",
    "                res.append(res[-1]*2)\n",
    "            elif op == \"+\":\n",
    "                res.append(res[-1]+res[-2])\n",
    "            else:\n",
    "                res.append(int(op))\n",
    "        return sum(res)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        ops = []\n",
    "        for i in operations:\n",
    "            if i == 'D':\n",
    "                ops.append(2 * ops[-1])\n",
    "            elif i == 'C':\n",
    "                ops.pop(-1)\n",
    "            elif i == '+':\n",
    "                ops.append(ops[-2] + ops[-1])\n",
    "            else:\n",
    "                ops.append(int(i))\n",
    "        return sum(ops)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: list[str]) -> int:\n",
    "        sums = 0\n",
    "        i = 0\n",
    "        while i < len(operations):\n",
    "            if operations[i] == 'C':\n",
    "                sums -= int(operations[i - 1])\n",
    "                operations.pop(i - 1)\n",
    "                operations.pop(i - 1)\n",
    "                i -= 2\n",
    "            elif operations[i] == 'D':\n",
    "                operations[i] = 2 * int(operations[i - 1])\n",
    "                sums += operations[i]\n",
    "            elif operations[i] == '+':\n",
    "                operations[i] = int(operations[i - 1]) + int(operations[i - 2])\n",
    "                sums += operations[i]\n",
    "            else:\n",
    "                sums += int(operations[i])\n",
    "            i += 1\n",
    "        return sums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        idx = 0\n",
    "        nums = [0] * 1010\n",
    "        for x in operations:\n",
    "            if x == '+':\n",
    "                nums[idx] = nums[idx - 1] + nums[idx - 2]\n",
    "            elif x == 'D':\n",
    "                nums[idx] = nums[idx - 1] * 2\n",
    "            elif x == 'C':\n",
    "                idx -= 2\n",
    "            else:\n",
    "                nums[idx] = int(x)\n",
    "            idx += 1\n",
    "        return sum(nums[:idx])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: list[str]) -> int:\n",
    "        sums = 0\n",
    "        i = 0\n",
    "        while i < len(operations):\n",
    "            if operations[i] == 'C':\n",
    "                sums -= int(operations[i - 1])\n",
    "                operations.pop(i - 1)\n",
    "                operations.pop(i - 1)\n",
    "                i -= 2\n",
    "            elif operations[i] == 'D':\n",
    "                operations[i] = 2 * int(operations[i - 1])\n",
    "                sums += operations[i]\n",
    "            elif operations[i] == '+':\n",
    "                operations[i] = int(operations[i - 1]) + int(operations[i - 2])\n",
    "                sums += operations[i]\n",
    "            else:\n",
    "                sums += int(operations[i])\n",
    "            i += 1\n",
    "        return sums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "\n",
    "        score = []\n",
    "\n",
    "        for i in range(len(operations)):\n",
    "\n",
    "            if operations[i] == \"C\":\n",
    "                score.pop()\n",
    "            elif operations[i] == \"D\":\n",
    "                score.append(score[-1]*2)\n",
    "            elif operations[i] == \"+\":\n",
    "                score.append(score[-1]+score[-2])\n",
    "            else:\n",
    "                score.append(int(operations[i]))\n",
    "\n",
    "        return sum(score)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        list1 = []\n",
    "        for index,item in enumerate(operations):\n",
    "            if (item.startswith('-') and item[1:] or item).isdigit():\n",
    "                list1.append(int(item))\n",
    "            elif item == \"C\":\n",
    "                list1.pop()\n",
    "            elif item == \"D\":\n",
    "                list1.append(list1[-1]*2)\n",
    "            elif item == \"+\":\n",
    "                list1.append(list1[-1]+list1[-2])\n",
    "        sum = 0\n",
    "        for item in list1:\n",
    "            sum += item\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        score = []\n",
    "        for i in range(len(operations)):\n",
    "            if(operations[i]=='+'):\n",
    "                score.append(score[-1]+score[-2])\n",
    "            elif(operations[i] == 'C'):\n",
    "                score.pop(-1)\n",
    "            elif(operations[i] == 'D'):\n",
    "                score.append(2*score[-1])\n",
    "            else:\n",
    "                score.append(int(operations[i]))\n",
    "        return sum(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations) -> int:\n",
    "        result = []\n",
    "        for i in range(len(operations)):\n",
    "            if operations[i]=='+':\n",
    "                result.append(result[-2]+result[-1])\n",
    "            elif operations[i]=='D':\n",
    "                result.append(result[-1]*2)\n",
    "            elif operations[i]=='C':\n",
    "                result.pop()\n",
    "            else:\n",
    "                result.append(int(operations[i]))\n",
    "        return sum(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        re = 0\n",
    "        re_list = []\n",
    "        for op in operations:\n",
    "            if op == \"C\":\n",
    "                del re_list[len(re_list)-1]\n",
    "            elif op == \"D\":\n",
    "                re_list.append(re_list[len(re_list)-1]*2)\n",
    "            elif op == \"+\":\n",
    "                re_list.append(re_list[len(re_list)-1]+re_list[len(re_list)-2])\n",
    "            else:\n",
    "                re_list.append(int(op))\n",
    "        for num in re_list:\n",
    "            re += num\n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        list_=[]\n",
    "        for i in operations:\n",
    "            if i=='C':\n",
    "                list_.pop()\n",
    "            elif i=='D':\n",
    "                list_.append(list_[-1]*2)\n",
    "            elif i=='+':\n",
    "                list_.append(list_[-1]+list_[-2])\n",
    "            else:\n",
    "                list_.append(int(i))\n",
    "        return sum(list_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, ops: List[str]) -> int:\n",
    "        score = []\n",
    "        for rec in ops:\n",
    "            if rec == 'D':\n",
    "                score.append(score[-1] * 2)\n",
    "            elif rec == 'C':\n",
    "                score.pop(-1)\n",
    "            elif rec == '+':\n",
    "                score.append(score[-1] + score[-2])\n",
    "            else:\n",
    "                score.append(int(rec))\n",
    "        \n",
    "        return sum(score)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        score_list = []\n",
    "\n",
    "        for i in operations:\n",
    "            if i == \"+\":\n",
    "                score_list.append(score_list[-1] + score_list[-2])\n",
    "               \n",
    "            elif i == \"D\":\n",
    "                score_list.append(score_list[-1] * 2)\n",
    "                \n",
    "            elif i == \"C\":\n",
    "                score_list.pop()\n",
    "\n",
    "            else:\n",
    "                score_list.append(int(i))\n",
    "            \n",
    "\n",
    "                \n",
    "            \n",
    "        return sum(score_list)\n",
    "\n",
    "            \n",
    "\n",
    "       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        stack=[]\n",
    "        for c in operations:\n",
    "            if c=='C':\n",
    "                stack.pop()\n",
    "            elif c=='D':\n",
    "                stack.append(stack[-1]*2)\n",
    "            elif c=='+':\n",
    "                stack.append(stack[-1]+stack[-2])\n",
    "            else:\n",
    "                stack.append(int(c))\n",
    "        return sum(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        ans = []\n",
    "        for i in operations:\n",
    "            if i == 'C':\n",
    "                ans.pop(-1)\n",
    "            elif i == 'D':\n",
    "                ans.append(ans[-1]*2)\n",
    "            elif i == '+':\n",
    "                ans.append(ans[-1]+ans[-2])\n",
    "            else:\n",
    "                ans.append(int(i))\n",
    "        return sum(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: list[str]) -> int:\n",
    "        sums = 0\n",
    "        i = 0\n",
    "        while i < len(operations):\n",
    "            if operations[i] == 'C':\n",
    "                sums -= int(operations[i - 1])\n",
    "                operations.pop(i - 1)\n",
    "                operations.pop(i - 1)\n",
    "                i -= 2\n",
    "            elif operations[i] == 'D':\n",
    "                operations[i] = 2 * int(operations[i - 1])\n",
    "                sums += operations[i]\n",
    "            elif operations[i] == '+':\n",
    "                operations[i] = int(operations[i - 1]) + int(operations[i - 2])\n",
    "                sums += operations[i]\n",
    "            else:\n",
    "                sums += int(operations[i])\n",
    "            i += 1\n",
    "        return sums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: list[str]) -> int:\n",
    "        sums = 0\n",
    "        i = 0\n",
    "        while i < len(operations):\n",
    "            if operations[i] == 'C':\n",
    "                sums -= int(operations[i - 1])\n",
    "                operations.pop(i - 1)\n",
    "                operations.pop(i - 1)\n",
    "                i -= 2\n",
    "            elif operations[i] == 'D':\n",
    "                operations[i] = 2 * int(operations[i - 1])\n",
    "                sums += operations[i]\n",
    "            elif operations[i] == '+':\n",
    "                operations[i] = int(operations[i - 1]) + int(operations[i - 2])\n",
    "                sums += operations[i]\n",
    "            else:\n",
    "                sums += int(operations[i])\n",
    "            i += 1\n",
    "        return sums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        score = []\n",
    "        for i in range(len(operations)):\n",
    "            if(operations[i]=='+'):\n",
    "                score.append(score[-1]+score[-2])\n",
    "            elif(operations[i] == 'C'):\n",
    "                score.pop(-1)\n",
    "            elif(operations[i] == 'D'):\n",
    "                score.append(2*score[-1])\n",
    "            else:\n",
    "                score.append(int(operations[i]))\n",
    "        return sum(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        ans = []\n",
    "        for op in operations:\n",
    "            if op == \"+\":\n",
    "                ans.append(ans[-1] + ans[-2])\n",
    "            elif op == \"D\":\n",
    "                ans.append(ans[-1] * 2)\n",
    "            elif op == \"C\":\n",
    "                ans.pop(-1)\n",
    "            else:\n",
    "                ans.append(int(op))\n",
    "        return sum(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        st = []\n",
    "        for op in operations:\n",
    "            if op == \"D\":\n",
    "                st.append(st[-1] * 2)\n",
    "            elif op == \"+\":\n",
    "                st.append(st[-1] + st[-2])\n",
    "            elif op == \"C\":\n",
    "                st.pop()\n",
    "            else:\n",
    "                st.append(int(op))\n",
    "        return sum(st)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, ops: List[str]) -> int:\n",
    "        ans=0\n",
    "        a=[]\n",
    "        for op in ops:\n",
    "            if op==\"+\":\n",
    "                b=a[-1]+a[-2]\n",
    "            elif op==\"D\":\n",
    "                b=2*a[-1]\n",
    "            elif op==\"C\":\n",
    "                ans-=a.pop()\n",
    "                continue\n",
    "            else:\n",
    "                b=int(op)\n",
    "            ans+=b\n",
    "            a.append(b)\n",
    "        return ans\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        top = 0\n",
    "        for i in operations:\n",
    "            if i == '+':\n",
    "                operations[top] = operations[top-2]+operations[top-1]\n",
    "                top += 1\n",
    "            elif i == 'D':\n",
    "                operations[top] = operations[top-1]*2\n",
    "                top += 1\n",
    "            elif i == 'C':\n",
    "                top -= 1\n",
    "            else:\n",
    "                operations[top] = int(i)\n",
    "                top += 1\n",
    "        res = 0\n",
    "        top -= 1\n",
    "        while top>=0:\n",
    "            res += operations[top]\n",
    "            top -=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        num = []\n",
    "        for i, c in enumerate(operations):\n",
    "            l = len(num)\n",
    "            if c == '+':\n",
    "                num.append(num[l - 1] + num[l - 2])\n",
    "            elif c == 'D':\n",
    "                num.append(num[l - 1] * 2)\n",
    "            elif c == 'C':\n",
    "                num.remove(num[l - 1])\n",
    "            else:\n",
    "                num.append(int(c))\n",
    "        return sum(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        points = list()\n",
    "\n",
    "        for ops in operations:\n",
    "            if ops == 'D':\n",
    "                points.append(points[-1] * 2)\n",
    "            elif ops == 'C':\n",
    "                points.pop(-1)\n",
    "            elif ops == '+':\n",
    "                points.append(points[-1] + points[-2])\n",
    "            else:\n",
    "                points.append(int(ops))\n",
    "        \n",
    "        return sum(points)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "nums = [0] * 1010\n",
    "class Solution:\n",
    "    def calPoints(self, ops: List[str]) -> int:\n",
    "        idx = 0\n",
    "        for x in ops:\n",
    "            if x == '+':\n",
    "                nums[idx] = nums[idx - 1] + nums[idx - 2]\n",
    "            elif x == 'D':\n",
    "                nums[idx] = nums[idx - 1] * 2\n",
    "            elif x == 'C':\n",
    "                idx -= 2\n",
    "            else:\n",
    "                nums[idx] = int(x)\n",
    "            idx += 1\n",
    "        return sum(nums[:idx])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "nums = [0] * 1000\n",
    "class Solution:\n",
    "    def calPoints(self, ops: List[str]) -> int:\n",
    "        idx = 0\n",
    "        for x in ops:\n",
    "            if x == '+':\n",
    "                nums[idx] = nums[idx - 1] + nums[idx - 2]\n",
    "            elif x == 'D':\n",
    "                nums[idx] = nums[idx - 1] * 2\n",
    "            elif x == 'C':\n",
    "                idx -= 2\n",
    "            else:\n",
    "                nums[idx] = int(x)\n",
    "            idx += 1\n",
    "        return sum(nums[:idx])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        res = []\n",
    "\n",
    "        for i in operations:\n",
    "            if i[0].isdigit():\n",
    "                res.append(int(i))\n",
    "            elif i[0] == '-':\n",
    "                res.append(-int(i[1:]))\n",
    "            elif i == 'C':\n",
    "                res.pop()\n",
    "            elif i == 'D':\n",
    "                res.append(res[-1]*2)\n",
    "            else:\n",
    "                res.append(res[-1]+res[-2])\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        l = []\n",
    "        for i in operations:\n",
    "            if i=='C': l.pop()\n",
    "            elif i=='D': l.append(l[-1]*2)\n",
    "            elif i=='+': l.append(l[-2]+l[-1])\n",
    "            else: l.append(int(i))\n",
    "        return sum(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calPoints(self, operations: List[str]) -> int:\n",
    "        result = []\n",
    "        l = len(operations)\n",
    "        for i in range(l):\n",
    "            if operations[i] == '+':\n",
    "                result.append(result[-1]+result[-2])\n",
    "            elif operations[i] == 'C':\n",
    "                result.pop()\n",
    "            elif operations[i] == 'D':\n",
    "                result.append(2*result[-1])\n",
    "            else:\n",
    "                result.append(int(operations[i]))\n",
    "        return sum(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "nums = [0] * 1010\n",
    "class Solution:\n",
    "    def calPoints(self, ops: List[str]) -> int:\n",
    "        idx = 0\n",
    "        for x in ops:\n",
    "            if x == '+':\n",
    "                nums[idx] = nums[idx - 1] + nums[idx - 2]\n",
    "            elif x == 'D':\n",
    "                nums[idx] = nums[idx - 1] * 2\n",
    "            elif x == 'C':\n",
    "                idx -= 2\n",
    "            else:\n",
    "                nums[idx] = int(x)\n",
    "            idx += 1\n",
    "        return sum(nums[:idx])\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
